//
//  DifficultType.swift
//  SkyApp
//
//  Created by 白翊 on 2021/12/29.
//

import UIKit

enum DifficultType {
    case easy
    case middle
    case difficult
    
    var desc: String {
        switch self {
        case .easy:
            return "简单"
        case .middle:
            return "中等"
        case .difficult:
            return "困难"
        }
    }
    
    var count: Int {
        switch self {
        case .easy:
            return 5
        case .middle:
            return 10
        case .difficult:
            return 20
        }
    }
}

enum QuestionType {
    case one
    case two
    case three
    
    var desc: String {
        switch self {
        case .one:
            return "花木马出现了几次？"
        case .two:
            return "花木马中有几个是倒立的？"
        case .three:
            return "指出花木马出现的位置"
        }
    }
    
    func judgeResult(choose: [Bool], datas: [MumaDisplay]) {
        var chooseIndexs = [Int]()
        for (index, value) in choose.enumerated() {
            if value {
                chooseIndexs.append(index)
            }
        }
        
        switch self {
        case .one:
            if let value = chooseIndexs.first {
                let count = datas.reduce(0, { $0 + ($1.isFlower ? 1 : 0) })
                if value == count {
                    //成功
                    success()
                }else {
                    fail(message: "很遗憾，选择错误，正确答案是：\(count)")
                }
            }
            break
        case .two:
            if let value = chooseIndexs.first {
                let count = datas.reduce(0, { $0 + ($1.isFlower && !$1.isUp ? 1 : 0) })
                if value == count {
                    //成功
                    success()
                }else {
                    fail(message: "很遗憾，选择错误，正确答案是：\(count)")
                }
            }
            break
        case .three:
            var flowerIndexs = [Int]()
            for (index, display) in datas.enumerated() {
                if display.isFlower {
                    flowerIndexs.append(index)
                }
            }
            
            if flowerIndexs == chooseIndexs {
                //成功
                success()
            }else {
                let result = flowerIndexs.reduce("", { $0 + "\($1) " })
                fail(message: "很遗憾，选择错误，正确答案是：\(result)")
            }
            
            break
        }
    }
    
    private func success() {
        ResultAlertView.show(img: "icon_success", desc: "恭喜你，回答正确！")
    }
    
    private func fail(message: String) {
        ResultAlertView.show(img: "icon_fail", desc: message)
    }
}
